using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Publicuse.Entity;
using Publicuse.Util;

namespace Publicuse.Export
{
  /// <summary>
  /// 数据字典信息辅助类
  /// </summary>
  public class DictionariesExport
  {
    /// <summary>
    /// 数据字典导出数据设置
    /// </summary>
    /// <param name="excelName"></param>
    /// <param name="fileName"></param>
    /// <param name="list"></param>
    /// <param name="msg"></param>
    /// <returns></returns>
    public static MemoryStream ExportData(
      string fileName,
      string excelName,
      List<Dictionaries> list,
      out string msg
    )
    {
      msg = string.Empty;
      MemoryStream ms = ExcelUtil.GetExcelUtil.InitExportData(
        fileName,
        excelName,
        list,
        (row, cell, sheet1, TextSytle, srow, hssfworkbook) =>
        {
          foreach (Dictionaries item in list)
          {
            srow++;
            row = sheet1.CreateRow(srow);

            //数据字典类型
            cell = row.CreateCell(0);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.dTypeName);

            //数据字典名称
            cell = row.CreateCell(1);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.dName);

            //数据字典编码
            cell = row.CreateCell(2);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.dCode);

            //数据字典值
            cell = row.CreateCell(3);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.dValue);


            //上级数据字典名称
            cell = row.CreateCell(4);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.dParentName);

            //状态
            cell = row.CreateCell(5);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.status == 1 ? "正常" : "停用");


            //创建时间
            cell = row.CreateCell(6);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.cTime.ToString("yyyy-MM-dd HH:mm:ss"));

            //修改时间
            cell = row.CreateCell(7);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.uTime?.ToString("yyyy-MM-dd HH:mm:sss"));

            //备注
            cell = row.CreateCell(8);
            cell.CellStyle = TextSytle;
            cell.SetCellValue(item.Remark);
          }
        },
        out msg,
        2
      );
      return ms;
    }
  }
}
